<?php
namespace app\admin\controller;

class VisitCount extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'VisitCount';
    }
    //
    public function xdel(){
        $vcid = $this->request->param('vcid', 0);
        if($vcid){
            $data = model($this->model)->where('vcid', '=', $vcid)->findOrEmpty()->toArray();
            if($data){
                $logs = [['pk' => 'vcid', 'value' => $vcid, 'row' => $data]];
                $data['total'] = $data['top'] = $data['middle'] = $data['bottom'] = $data['windows'] = $data['iphone'] = $data['android'] = $data['other'] = 0;
                $rs = model($this->model)->update($data);
                if($rs){
                    $this->logAll($logs);
                    $return = $this->response('清空成功', false, url($this->request->controller().'/index'), 200);
                    return json($return);
                }
            }
        }
        $return = $this->response('清空失败', false, '', 300);
        return json($return);
    }
    //
    protected function _before_index(){
        $thead = [
            'vcid' => 'ID',
            'aid' => '推广名称',
            'domain' => '推广域名',
            'tname' => '二级目录',
            'total' => '误点或爬虫',
            'top' => '实际访问量',
            //'xxx' => '1/4位置访问量',
            'middle' => '中部访问量',
            //'yyy' => '3/4位置访问量',
            'bottom' => '底部访问量',
            'xdate' => '统计时间',
        ];
        $this->assign('thead', $thead);
    }
    //
    protected function _filter($map){
        $tmp = [];
        foreach($map as $key => $row){
            if($row[0] == 'aid'){
                $tmp[] = ['xname', $row[1], $row[2]];
                unset($map[$key]);
            }elseif($row[0] == 'top'){
                $tmp[] = ['tname', $row[1], $row[2]];
                unset($map[$key]);
            }elseif($row[0] == 'bottom'){
                $didArr = model('domain')->where('domain', 'like', $row[2])->column('did');
                $tmp[] = $didArr ? ['did', 'in', $didArr] : ['did', 'in', []];
                unset($map[$key]);
            }
        }
        if($tmp){
            $aidArr = model('Advert')->where($tmp)->column('aid');
            $map[] = $aidArr ? ['aid', 'in', $aidArr] : ['aid', 'in', []];
        }
        $map ? $map = array_values($map) : null;
        return $map;
    }
    //
    protected function _after_list($list){
        $aidArr = [];
        foreach($list as $row){
            $aidArr[] = $row['aid'];
        }
        $tmp = model('Advert')->where('aid', 'in', $aidArr)->column('xname,tname,did', 'aid');
        $table = db('domain');
        foreach($list as $key => $row){
            $list[$key]['total'] = ($row['total'] > $row['top']) ? ($row['total'] - $row['top']) : 0;
            //$list[$key]['xxx'] = $list[$key]['yyy'] = 0;
            $list[$key]['middle'] = $row['middle'].' ('.($row['top'] == 0 ? 0 : (round($row['middle']/$row['top'], 2))*100).'%)';
            $list[$key]['bottom'] = $row['bottom'].' ('.($row['top'] == 0 ? 0 : (round($row['bottom']/$row['top'], 2))*100).'%)';
            $list[$key]['aid'] = $tmp[$row['aid']]['xname'] ?? $row['aid'];
            $list[$key]['tname'] = $tmp[$row['aid']]['tname'] ?? '';
            $list[$key]['domain'] = isset($tmp[$row['aid']]['did']) ? $table->where('did', '=', $tmp[$row['aid']]['did'])->value('domain', '') : '';
        }
        return $list;
    }
    //
    protected function _filter_del(){
        return [];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'vcid', 'value' => $row['vcid'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}